package com.huluobo.a2109mvpimdemo.contact;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huluobo.a2109mvpimdemo.R;
import com.huluobo.a2109mvpimdemo.adapter.ContactAdapter;
import com.huluobo.a2109mvpimdemo.base.BaseFragment;
import com.huluobo.a2109mvpimdemo.view.SlideBar;

import java.util.ArrayList;
import java.util.List;

public class MyContractFragment extends BaseFragment<ContactContract.View, ContactPresenter> implements ContactContract.View {

    private RecyclerView recyclerView;
    private List<ContactListItemBean> list = new ArrayList<>();
    private ContactAdapter contactAdapter;

    private SlideBar slideBar;
    private TextView section;

    private SwipeRefreshLayout swipeRefreshLayout;

    private Button contact_btn_search;
    private EditText contact_ed_search;

    @Override
    protected int bindingLayoutId() {
        return R.layout.fragment_my_contract;
    }

    @Override
    protected void initData() {
        presenter = new ContactPresenter();
        presenter.loadContacts();
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.contact_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        contactAdapter = new ContactAdapter(list, getContext());
        recyclerView.setAdapter(contactAdapter);

        slideBar = findViewById(R.id.contact_slideBar);
        section = findViewById(R.id.contact_section);
        contact_btn_search = findViewById(R.id.contact_btn_search);
        contact_ed_search = findViewById(R.id.contact_ed_search);
        contact_btn_search.setOnClickListener(v -> {
            presenter.findContact(contact_ed_search.getText().toString());
        });

        swipeRefreshLayout = findViewById(R.id.contact_swipe);
        slideBar.setOnSectionChangeListener(new SlideBar.OnSectionChangeListener() {
            @Override
            public void onSectionChange(String firstLetter) {
                section.setVisibility(View.VISIBLE);
                section.setText(firstLetter);
                moveToLetterPosition(firstLetter);
            }

            @Override
            public void onSlideFinish() {
                section.setVisibility(View.GONE);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(() -> {
            presenter.loadContacts();
        });
    }

    private void moveToLetterPosition(String firstLetter) {
        for (int i = 0; i < list.size(); i++) {
            //如果对比首字符相等
            if ((list.get(i).getFirstLetter() + "").equals(firstLetter)) {
                recyclerView.scrollToPosition(i);//让recycleView滚动到对应的下标位置
            }
        }
    }

    @Override
    public void onLoadContactsSuccess(List<ContactListItemBean> contactListItemBeanList) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                list.clear();
                list.addAll(contactListItemBeanList);
                contactAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onLoadContactsFailed(String error) {
        getActivity().runOnUiThread(() -> Toast.makeText(getActivity(), "读取联系人错误:" + error, Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onFindContactSuccess(List<ContactListItemBean> contactListItemBeanList) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                contactAdapter = new ContactAdapter(contactListItemBeanList, getContext());
                recyclerView.setAdapter(contactAdapter);
            }
        });

    }

    @Override
    public void onFindContactFailed() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                contactAdapter = new ContactAdapter(list, getContext());
                recyclerView.setAdapter(contactAdapter);
            }
        });

    }
}